package com.weipeep.sys.controller;


import java.io.IOException;
import java.io.OutputStream;
import java.io.PrintWriter;
import java.net.URLEncoder;
import java.text.SimpleDateFormat;
import java.util.*;

import com.baomidou.mybatisplus.core.conditions.Wrapper;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.google.common.collect.Lists;
import com.weipeep.common.utils.DateUtils;
import com.weipeep.common.utils.poi.ExcelHelper;
import com.weipeep.common.utils.poi.ExcelWriter;
import com.weipeep.common.utils.poi.UserEntity;
import com.weipeep.sys.domain.SignInDo;
import com.weipeep.sys.service.StaffService;
import com.weipeep.sys.service.StudentService;
import com.weipeep.sys.vo.SignVo;
import com.weipeep.sys.vo.StaffVo;
import com.weipeep.sys.vo.StudentVo;
import org.apache.poi.ss.formula.functions.T;
import org.apache.poi.ss.usermodel.Workbook;
import org.apache.shiro.authz.annotation.RequiresPermissions;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;
import com.weipeep.sys.domain.SignDO;
import com.weipeep.sys.service.SignService;
import com.weipeep.common.utils.Result;

import javax.servlet.http.HttpServletResponse;

/**
 * 
 * <pre>
 * 
 * </pre>
 * <small> 2018-04-16 21:15:48 | wangyun</small>
 */
@Controller
@RequestMapping("/sys/sign")
public class SignController {
	@Autowired
	private SignService signService;
	@Autowired
	private StaffService staffService;
	@GetMapping()
	@RequiresPermissions("sys:sign:sign")
	String Sign(){
	    return "sys/sign/sign";
	}
	
	@ResponseBody
	@GetMapping("/page")
	@RequiresPermissions("sys:sign:sign")
	public Result<Page<SignDO>> page(Integer pageNumber, Integer pageSize, SignDO signDTO){
		// 查询列表数据
        Page<SignDO> page = new Page<>(pageNumber, pageSize);
        
        Wrapper<SignDO> wrapper = new QueryWrapper<SignDO>(signDTO);
        page = (Page<SignDO>) signService.page(page, wrapper);
        return Result.ok(page);
	}
	@ResponseBody
	@GetMapping("/list")
	@RequiresPermissions("sys:sign:sign")
	public Result<List<SignVo>> list(StaffVo staffVo){
		// 查询列表数据
		List<StaffVo> studentVoList = staffService.selectStaffList(staffVo);
		Map<Long,SignDO> signDOMap = signService.fetchSigned(DateUtils.getStartTime(),DateUtils.getEndTime());
		List<SignVo> signVoList = Lists.newArrayList();
		studentVoList.forEach(o->{
			SignVo signVo = new SignVo();
			signVo.setId(o.getUserId());
			signVo.setRealName(o.getRealName());
			signVo.setStudentNumber(o.getStaffNo());
			SignDO signDO = signDOMap.get(o.getUserId());
			if(signDO !=null){
				signVo.setSignInTime(signDO.getSignInTime());
				signVo.setSignOutTime(signDO.getSignOutTime());
			}
			signVoList.add(signVo);
		});
		return Result.ok(signVoList);
	}
	@GetMapping("/add")
	@RequiresPermissions("sys:sign:add")
	String add(){
	    return "sys/sign/add";
	}

	@GetMapping("/edit/{id}")
	@RequiresPermissions("sys:sign:edit")
	String edit(@PathVariable("id") Long id,Model model){
		SignDO sign = signService.getById(id);
		model.addAttribute("sign", sign);
	    return "sys/sign/edit";
	}
	
	/**
	 * 保存
	 */
	@ResponseBody
	@PostMapping("/save")
	@RequiresPermissions("sys:sign:add")
	public Result<String> save( SignDO sign){
		if(signService.save(sign)){
			return Result.ok();
		}
		return Result.fail();
	}
	/**
	 * 修改
	 */
	@ResponseBody
	@RequestMapping("/update")
	@RequiresPermissions("sys:sign:edit")
	public Result<String>  update( SignDO sign){
		signService.updateById(sign);
		return Result.ok();
	}
	
	/**
	 * 删除
	 */
	@PostMapping( "/remove")
	@ResponseBody
	@RequiresPermissions("sys:sign:remove")
	public Result<String>  remove( Long id){
		if(signService.removeById(id)){
            return Result.ok();
		}
		return Result.fail();
	}

	/**
	 * 签到或签退
	 */
	@PostMapping( "/signOrOut")
	@ResponseBody
	@RequiresPermissions("sys:sign:signOrOut")
	public Result<String> signOrOut( Long userId,Integer signType){
		if(signService.signOrOut(userId,signType)){
			return Result.ok();
		}
		return Result.fail();
	}

	/**
	 * 删除
	 */
	@PostMapping( "/batchRemove")
	@ResponseBody
	@RequiresPermissions("sys:sign:batchRemove")
	public Result<String>  remove(@RequestParam("ids[]") Long[] ids){
		signService.removeByIds(Arrays.asList(ids));
		return Result.ok();
	}

	/***
	 * 导出签到数据
	 */
	@GetMapping("/signIn")
	public void signIn(Date beginTime, Date endTime, HttpServletResponse response){
		SimpleDateFormat time=new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
		ExcelHelper<SignInDo> excelHelper = new ExcelHelper();
		List<SignInDo> list = signService.getSignInList(beginTime,endTime);
		excelHelper.writeExcel(response,list, new ExcelWriter<SignInDo>() {
			@Override
			public void writeRow(SignInDo obj) {
				addCell("姓名", obj.getName());
				addCell("签到时间", obj.getInTime()==null?"":time.format(obj.getInTime()));
				addCell("签退时间", obj.getOutTime()==null?"":time.format(obj.getOutTime()));
			}
		});
	}
}
